package com.tracy.cprinter;

import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.bridge.ReadableType;
import com.facebook.react.bridge.WritableArray;
import com.gprinter.command.FactoryCommand;
import com.tracy.cprinter.printer.UsbDeviceConnectManager;

import java.io.UnsupportedEncodingException;
import java.util.Vector;

public class RNCPrinterModule extends ReactContextBaseJavaModule {
    private static final String TAG = "RNCPrinterModule";
    private final ReactApplicationContext reactContext;

    public RNCPrinterModule(ReactApplicationContext reactContext) {
        super(reactContext);
        this.reactContext = reactContext;
        UsbDeviceConnectManager.getInstance().setReactContext(reactContext);
    }

    @Override
    public String getName() {
        return "RNCPrinterModule";
    }

    /**
     * 获取usb设备列表
     */
    @ReactMethod
    public void getUsbDeviceList(Promise promise) {
        WritableArray list = UsbDeviceConnectManager.getInstance().getUsbDeviceList();
        promise.resolve(list);
    }

    /**
     * usb打印机连接
     */
    @ReactMethod
    public void usbDeviceConnect(String device) {
        UsbDeviceConnectManager.getInstance().usbDeviceConnect(device);
    }

    /**
     * usb打印机连接断开
     */
    @ReactMethod
    public void usbDeviceDisconnect() {
        UsbDeviceConnectManager.getInstance().stopConnectWithEvent();
    }

    @ReactMethod
    public void checkPrintStatus(ReadableArray commands, Promise promise) {
        if (!UsbDeviceConnectManager.getInstance().isOpenPort) {
            promise.reject("1001", "打印机未连接");
            return;
        }
        if (commands == null || commands.size() == 0) {
            promise.reject("1002", "指令不能为空");
            return;
        }

        byte[] tsc = {0x1b, '!', '?'};

        Vector<Byte> data = new Vector<>();
        for (int i = 0; i < tsc.length; i++) {
            data.add(tsc[i]);
        }
        UsbDeviceConnectManager.getInstance().sendDataImmediately(data);
        promise.resolve(null);
    }

    /**
     * 更换打印模式
     */
    @ReactMethod
    public void changeWorkMode(int type, Promise promise) {
        if (type != 0 && type != 1 && type != 2) {
            promise.reject("1001", "模式参数错误，0票据，1标签，2面单");
            return;
        }

        if (!UsbDeviceConnectManager.getInstance().isOpenPort) {
            promise.reject("1002", "打印机未连接");
            return;
        }

        byte[] bytes = FactoryCommand.changeWorkMode(type);
        Vector<Byte> data = new Vector<>();
        for (int i = 0, len = bytes.length; i < len; i++) {
            data.add(bytes[i]);
        }
        UsbDeviceConnectManager.getInstance().sendDataImmediately(data);
        promise.resolve(null);
    }

    /**
     * 设置命令编码
     */
    @ReactMethod
    public void setCommandEncode(String str) {
        UsbDeviceConnectManager.getInstance().encode = str;
    }

    /**
     * 打印数据
     */
    @ReactMethod
    public void sendData(ReadableArray commands, Promise promise) {
        if (!UsbDeviceConnectManager.getInstance().isOpenPort) {
            promise.reject("1001", "打印机未连接");
            return;
        }
        if (commands == null || commands.size() == 0) {
            promise.reject("1002", "指令不能为空");
            return;
        }

        Vector<Byte> data = new Vector<>();
        for (int i = 0, len = commands.size(); i < len; i++) {
            ReadableType type = commands.getType(i);
            if (type == ReadableType.String) {
                String str = commands.getString(i);
                if (!str.equals("")) {
                    byte b = 27;
                    byte[] bs = null;
                    try {
                        bs = str.getBytes(UsbDeviceConnectManager.getInstance().encode);
                    } catch (UnsupportedEncodingException var5) {
                        var5.printStackTrace();
                        promise.reject("1003", "命令编码"+UsbDeviceConnectManager.getInstance().encode+"错误");
                        return;
                    }
                    if (bs != null) {
                        for (int n = 0; n < bs.length; ++n) {
                            data.add(bs[n]);
                        }
                    }
                }
            } else if (type == ReadableType.Array) {
                ReadableArray item = commands.getArray(i);
                for (int n = 0; n < item.size(); n++) {
                    ReadableType itemType = item.getType(n);
                    if (itemType == ReadableType.Number) {
                        int itemNum = item.getInt(n);
                        data.add((byte)itemNum);
                    }
                }
            }
        }

        UsbDeviceConnectManager.getInstance().sendDataImmediately(data);
        promise.resolve(null);
    }
}
